package com.yuecai.modules.yc_version;

import com.yuecai.modules.yc_version.service.YcVersionService;
import com.yuecai.modules.yc_version.service.dto.YcVersionDto;
import com.yuecai.utils.VersionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;


@RestController
public class VersionController {


    @Autowired
    private YcVersionService ycVersionService;

    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping("/auth/api/version")
    public ResponseEntity<Object> findVersion(@RequestBody HashMap<String,String> mps){
        String version = mps.get("version");
        String platfrom = mps.get("type");
        String versionCode = (String) redisTemplate.opsForHash().get("version"+"_"+platfrom, "versionCode");
        String downloadUrl = (String) redisTemplate.opsForHash().get("version"+"_"+platfrom, "downloadUrl");
        String versionInfo = (String) redisTemplate.opsForHash().get("version"+"_"+platfrom, "versionInfo");
        Boolean forceUpdate = (Boolean) redisTemplate.opsForHash().get("version"+"_"+platfrom, "forceUpdate");
        String versionName = (String) redisTemplate.opsForHash().get("version"+"_"+platfrom, "versionName");
        Long expire = redisTemplate.getExpire("version" + "_" + platfrom);

        if (StringUtils.isBlank(versionCode)||
                StringUtils.isBlank(versionCode)||StringUtils.isBlank(downloadUrl)||
                StringUtils.isBlank(versionInfo)||StringUtils.isBlank(versionName)||forceUpdate==null){
            YcVersionDto byLimit1 = ycVersionService.findByLimit1(platfrom);
            if (byLimit1!=null){
                downloadUrl = byLimit1.getDownloadUrl();
                versionCode = byLimit1.getVersionCode();
                versionInfo = byLimit1.getVersionInfo();
                forceUpdate = byLimit1.getForceUpdate();
                versionName = byLimit1.getVersionName();
                redisTemplate.opsForHash().put("version"+"_"+platfrom, "versionCode",versionCode);
                redisTemplate.opsForHash().put("version"+"_"+platfrom, "downloadUrl",downloadUrl);
                redisTemplate.opsForHash().put("version"+"_"+platfrom, "versionInfo",versionInfo);
                redisTemplate.opsForHash().put("version"+"_"+platfrom, "forceUpdate",forceUpdate);
                redisTemplate.opsForHash().put("version"+"_"+platfrom, "versionName",versionName);
            }
        }
        //0代表相等，1代表左边大，-1代表右边大
        int i = VersionUtils.compareVersion(version, versionCode);
        HashMap<String, Object> map = new HashMap<>();
        if (i==-1){
            map.put("versionCode",versionCode);
            map.put("downloadUrl",downloadUrl);
            map.put("versionInfo",versionInfo);
            map.put("forceUpdate",forceUpdate);
            map.put("versionName",versionName);
        }else {
        }
        return ResponseEntity.ok(map);
    }
}
